﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using _MetalSlug.MyCharacters.Characters;

namespace _MetalSlug.Weapons.Units
{
    public class Boom : Bullet
    {
        List<Texture2D> _explode;
        List<Texture2D> _boom;

        public const int DestHeight = 400;
        public const int TimeDelay = 50;

        Random rand = new Random();

        public enum State
        {
            DROP,
            EXPLODE
        }

        State currentState;

        public State CurrentState
        {
            get { return currentState; }
            set 
            {
                currentState = value;
                SetTextures();
            }
        }

        public Boom()
        {
        }

        public Boom(Vector2 topleft, Vector2 size, Vector2 _Speed)
        {
            Type = TYPE_BULLET.BOOM_BULLET;  
            InitialAttribute(topleft,size,_Speed);
        }

        public override void LoadContent()
        {
            _explode = GameUtil.LoadTextures("Boom/Explode", 16);
            _boom = GameUtil.LoadTextures("Boom/Boom", 3);
            MySprite temp = new MySprite(_boom, TopLeft);
            temp.Size = Size;
            Sprites.Add(temp);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Sprites[0].TopLeft = TopLeft;
            if(Visible == true)
            {
                base.Draw(spriteBatch);
            }       
        }

        public override void Update(GameTime gameTime)
        {
            if (gameTime.TotalGameTime.Milliseconds % TimeDelay  != 0)
            {
                return;
            }

            if (CurrentState == State.DROP)
            {
                TopLeft += Direction * Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (TopLeft.Y > DestHeight)
                {
                    CurrentState = Boom.State.EXPLODE;
                    float newX, newY;
                    newY = Sprites[0].TopLeft.Y + _boom[0].Height - _explode[0].Height;
                    newX = Sprites[0].TopLeft.X + _boom[0].Width / 2 - _explode[0].Width / 2;
                    TopLeft = new Vector2(newX, newY);
                }

            }
            else
            {
                if (Sprites[0].isLastFrame())
                {
                    Visible = false;
                }
            }

            if (Visible)
            {
                Sprites[0].nextFrame();
            }
        }

        public override void Fire(Vector2 theStartPosition, Vector2 theDirection)
        {
            TopLeft = theStartPosition;
            Direction = theDirection;
        }


        public override Bullet Clone()
        {
            Bullet boomBullet = new Boom();
            boomBullet.Copy(this);
            return boomBullet;
        }

        public override void Copy(Bullet Origin)
        {
            base.Copy(Origin);
            Boom boom = (Boom)Origin;
            this._boom = boom._boom;
            this._explode = boom._explode;
            this.rand = new Random();
            CurrentState = State.DROP;
        }

        public Boolean isExplored()
        {
            if (Sprites[0].Textures == _explode && Sprites[0].isLastFrame())
            {
                return true;
            }
            return false;
        }

        public void SetTextures()
        {
            switch(CurrentState)
            {
                case State.DROP:
                    Sprites[0].Textures = _boom;
                    break;

                case State.EXPLODE:
                    Sprites[0].Textures = _explode;
                    break;
            }
        }
    }

}
